package com.apestech.api.exp;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import java.util.ArrayList;

public class ExpTable {

    public class FkTable{
        private String id;
        private String db;  //特殊目标库

        public FkTable(JSONObject map){
            id=map.getString("id");
            db=map.getString("db");
        }

        public String getId() {
            return id;
        }

        public String getDb() {
            return db;
        }
    }

    public class Check {
        private String id;
        private String type;
        private String defaultValue;

        public Check(JSONObject map){
            id=map.getString("id");
            type=map.getString("type");
            defaultValue=map.getString("defaultValue");
        }

        public String getId() {
            return id;
        }

        public String getType() {
            return type;
        }

        public String getDefaultValue() {
            return defaultValue;
        }
    }

    public class Field{
        private String id;
        private String field;
        private String out;

        public Field(String id, JSONObject map){
            this.id=id;
            field=map.getString("field");
            out=map.getString("out");
        }

        public String getId() {
            return id;
        }

        public String getField() {
            return field;
        }

        public String getOut() {
            return out;
        }
    }

    private String id;
    private String source;
    private String sdatabase;
    private String sql;
    private ArrayList<FkTable> fkTable;
    private JSONObject check=new JSONObject();
    private JSONObject field=new JSONObject();

    public ExpTable(JSONObject map){
        fkTable=new ArrayList<FkTable>();
        ArrayList<String> klist=map.getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String k=klist.get(i);
            switch (k){
                case "id":{
                    id=map.getString("id");
                    break;
                }
                case "source":{
                    source=map.getString("source");
                    if(source==null) source=map.getString("id");
                    break;
                }
                case "sdatabase":{
                    sdatabase=map.getString("sdatabase");
                    break;
                }
                case "sql":{
                    sql=((JSONObject)map.get("sql")).getString("sql");
                    break;
                }
                case "fkkey":{
                    JSONArray flist;
                    if(map.get("fkkey") instanceof JSONArray)
                        flist= (JSONArray) map.get("fkkey");
                    else {
                        flist=new JSONArray();
                        flist.add(map.get("fkkey"));
                    }
                    int flen=flist.size();
                    for(int f=0;f<flen;f++){
                        addFkTable(flist.get(f));
                    }
                    break;
                }
                case "check":{
                    JSONArray clist;
                    if(map.get("check") instanceof JSONArray)
                        clist= (JSONArray) map.get("check");
                    else {
                        clist=new JSONArray();
                        clist.add(map.get("check"));
                    }
                    int clen=clist.size();
                    for(int c=0;c<clen;c++){
                        addCheck((JSONObject) clist.get(c).get("field"));
                    }
                    break;
                }
                default:{
                    addField(k, (JSONObject) map.get(k));
                }
            }
        }
    }

    public void addFkTable(JSONObject map){
        FkTable fk=new FkTable(map);
        fkTable.add(fk);
    }

    public void addCheck(JSONObject map){
        String id=map.getString("id");
        check.put(id, new Check(map));
    }

    public void addField(String id, JSONObject map){
        field.put(id, new Field(id, map));
    }

    public String getId() {
        return id;
    }

    public String getSource() {
        return source;
    }

    public String getSdatabase() {
        return sdatabase;
    }

    public ArrayList<FkTable> getFkTable() {
        return fkTable;
    }

    public String getSql() {
        return sql;
    }

    public JSONObject getCheck() {
        return check;
    }

    public JSONObject getField() {
        return field;
    }
}
